<?php
function brt_block_preprocess_solveto_header(&$vars) {
    global $base_url;
    global $user;
    $brt_data = isset($vars['brt_block']) ? $vars['brt_block'] : null;
    if($brt_data != null) {
        if(isset($brt_data['menu_select'])) {
            $menu_tree = menu_tree_all_data($brt_data['menu_select']);
            $menu_tree_output = menu_tree_output($menu_tree);
            $vars['menu_expanded'] = $menu_tree_output;
            $menu_tree_output['#theme_wrappers'][0] = 'menu_tree__responsive';
            $vars['menu_mobile'] = $menu_tree_output;

            //Sticky
            $vars['sticky_class'] = '';
            if(isset($brt_data['menu_enable_sticky']) && $brt_data['menu_enable_sticky'] == 1) {
                $vars['sticky_class'] = 'sticky_header';
            }
            //Logo
            $vars['logo'] = $base_url . '/'. drupal_get_path('theme','solveto').'/logo.png';
            if($brt_data['menu_enable_custom_logo'] == 1) {
                if(module_exists('media')) {
                    if($brt_data['menu_custom_logo_media_file']) {
                        $file_upload = theme_get_setting('menu_custom_logo_media_file');
                        if(!empty($file_upload)) {
                            $file = json_decode($file_upload);
                            $vars['logo'] = $file->url;
                        }
                    }
                } else {
                    if ($brt_data['menu_custom_logo_form_file']) {
                        $vars['logo'] =  file_create_url(file_build_uri($brt_data['menu_custom_logo_form_file']));
                    }
                }
            } else {
                if(theme_get_setting('default_logo') == 0) {
                    if(module_exists('media')) {
                        if($file_upload = theme_get_setting('logo_normal_media_file')) {
                            if(!empty($file_upload)) {
                                $file = json_decode($file_upload);
                                if($file) {
                                    $vars['logo'] = $file->url;
                                }
                            }
                        }
                    } else {
                        if ($logo_file = theme_get_setting('logo_normal_form_file')) {
                            $vars['logo'] =  file_create_url(file_build_uri($logo_file));
                        }
                    }
                }
            }
            // Search
            $vars['search'] = '';
            if($brt_data['menu_enable_search'] == 1) {
                $search = module_invoke('search', 'block_view', 'search');
                $search['content']['#prefix'] = '<div class="input-append">';
                $search['content']['#suffix'] = '</div>';
                $search['content']['search_block_form']['#suffix'] = '<a href="#" class="close_"><i class="moon-close"></i></a>';
                $search['content']['search_block_form']['#attributes']['class'][] = '';
                $search['content']['search_block_form']['#attributes']['placeholder'] = 'Search...';
                $vars['search'] = $search['content'];
            }
        }
        // Top & Bottom settings
        $top_left = $brt_data['top_left_display'];
        $top_right = $brt_data['top_right_display'];
        $bottom_left = $brt_data['bottom_left_display'];
        $bottom_right = $brt_data['bottom_right_display'];
        $vars['top_left'] = '';
        $vars['top_right'] = '';
        $vars['bottom_left'] = '';
        $vars['bottom_right'] = '';
        $vars['header_class'] = 'header_1';
        if(isset($brt_data['menu_style'])) {
            $vars['header_class'] = $brt_data['menu_style'];
        }
        //User Block
        $user_block_display = '';
        $user_block = module_invoke('user', 'block_view', 'login');
        $render_user_block = drupal_render($user_block['content']);
        if($user->uid == 0) {
            $user_block_display = '
                <div class="login small_widget">
                    <div class="widget_activation"><a href="#" class="open-widget" data-box="login">'.t('Welcome Guest. Login').'</a>
                    </div>
                    <div class="top_nav_sub login">
                        <div class="sub-loggin">
                            '.$render_user_block.'
                        </div>
                    </div>
                </div>
            ';
        } else {
            $user_block_display = '
            
                <div class="login small_widget">
                    <div class="widget_activation">
                        <div class="aaaa">
                            <a href="'.url("user/".$user->uid).'">'.t('Welcome').', '.$user->name.'</a>
                        </div>
                    </div>
                </div>
            ';
        }
        //Cart Block
        $cart_block = '';
        if(module_exists('commerce') && module_exists('commerce_cart')) {
            $cart_invoke =  module_invoke('commerce_cart', 'block_view', 'cart');
            $cart_block = $cart_invoke['content'];
        }


        //Language Switcher
        $lang_block = '';
        if(module_exists('locale')) {
            if(module_exists('lang_dropdown')) {
                $lang =  module_invoke('lang_dropdown', 'block_view', 'language');
                $lang_block = drupal_render($lang['content']);
            } else {
                $lang =  module_invoke('locale', 'block_view', 'language');
                $lang_block = drupal_render($lang['content']);
            }
        }
        
        // Social 
        $social_block = '';
        if($brt_data['hd_social_us_data'] != null) {
            $json_decode = drupal_json_decode($brt_data['hd_social_us_data']);
            $count = count($json_decode);
            $social_block .= '
                            <ul class="socials">';
                                foreach($json_decode as $key => $value) {
                                    $social_block .= '<li><a href="'.$value[1]['value'].'"><i class="'.str_replace("|"," ",$value[0]['value']).'"></i></a></li>';
                                    
                                }
            $social_block .= '
                            </ul>
                        ';
        } 
        // Address
        $address_block_1 = '';
        $address_block_2 = '';
        if(isset($brt_data['address_text_1']) || isset($brt_data['address_text_2'])) {
            if(!empty($brt_data['address_text_1'])) {
                $address_block_1 = $brt_data['address_text_1'];
            }
            if(!empty($brt_data['address_text_2'])) {
                $address_block_2 = $brt_data['address_text_2'];
            }     
        }

        // Top Left & Right
        if(strlen($top_left['user']) > 1) {
            $vars['top_left'] .= '<div class="widget">'.$user_block_display.'</div>';
        }
        if(strlen($top_right['user']) > 1) {
            $vars['top_right'] .= '<div class="widget">'.$user_block_display.'</div>';
        }
        if(module_exists('commerce') && module_exists('commerce_cart')) {
            if(isset($top_left['cart']) && strlen($top_left['cart']) > 1) {
                $vars['top_left'] .= '
                <div class="widget">
                    <div class="headecart small_widget">
                        <div class="widget_activation">
                            <a href="#" data-box="headcart" class="open-widget nr">'.t('Shoping Cart').'</a><span class=""></span>
                        </div>
                        <div class="top_nav_sub headcart">
                            <div class="cart">
                                '.$cart_block.'
                            </div>
                        </div>
                    </div>
                </div>';
            }
            if(isset($top_right['cart']) && strlen($top_right['cart']) > 1) {
                $vars['top_right'] .= '
                <div class="widget">
                    <div class="headecart small_widget">
                        <div class="widget_activation">
                            <a href="#" data-box="headcart" class="open-widget nr">'.t('Shoping Cart').'</a><span class=""></span>
                        </div>
                        <div class="top_nav_sub headcart">
                            <div class="cart">
                                <div class="content">
                                    '.$cart_block.'
                                </div>
                            </div>
                        </div>
                    </div>
                </div>';
            }
        }
        if(module_exists('locale')) {
            if(isset($top_left['language']) && strlen($top_left['language']) > 1) {
                $vars['top_left'] .= '
                <div class="widget widget_icl_lang_sel_widget">
                    <div id="lang_sel">
                        '.$lang_block.'
                    </div>
                </div>';
            }

            if(isset($top_right['language']) && strlen($top_right['language']) > 1) {
                $vars['top_right'] .= '
                <div class="widget widget_icl_lang_sel_widget">
                    <div id="lang_sel">
                        '.$lang_block.'
                    </div>
                </div>';
            }
        }

        if(strlen($top_left['social']) > 1) {
            $vars['top_left'] .= '
                <div id="text-4" class="widget widget_text">
                    <div class="textwidget">'.t('Follow us').':</div>
                </div>

                <div class="widget social_widget">
                    <div class="row-fluid social_row">
                        <div class="span12">
                            '.$social_block.'
                        </div>
                    </div>
                </div>';
        }
        if(strlen($top_right['social']) > 1) {
            $vars['top_right'] .= '
                <div id="text-4" class="widget widget_text">
                    <div class="textwidget">'.t('Follow us').':</div>
                </div>

                <div class="widget social_widget">
                    <div class="row-fluid social_row">
                        <div class="span12">
                            '.$social_block.'
                        </div>
                    </div>
                </div>';
        }
        if(strlen($top_left['address']) > 1) {
            $vars['top_left'] .= '                
                <div class="widget widget_text">
                    <div class="textwidget">'.$address_block_1.'</div>
                </div>
                <div class="widget widget_text">
                    <div class="textwidget">'.$address_block_2.'</div>
                </div>';
        }
        if(strlen($top_right['address']) > 1) {
            $vars['top_right'] .= '
                <div class="widget widget_text">
                    <div class="textwidget">'.$address_block_1.'</div>
                </div>
                <div class="widget widget_text">
                    <div class="textwidget">'.$address_block_2.'</div>
                </div>';
        }

        // Bottom Left & RIght
        if(strlen($bottom_left['user']) > 1) {
            $vars['bottom_left'] .= $user_block_display;
        }
        if(strlen($bottom_right['user']) > 1) {
            $vars['bottom_right'] .= $user_block_display;
        }
        if(module_exists('commerce') && module_exists('commerce_cart')) {
            if(isset($bottom_left['cart']) && strlen($bottom_left['cart']) > 1) {
                $vars['bottom_left'] .= '

                    <div class="headecart small_widget">
                        <div class="widget_activation">
                            <a href="#" data-box="headcart" class="open-widget nr">'.t('Shoping Cart').'</a><span class=""></span>
                        </div>
                        <div class="top_nav_sub headcart">
                            <div class="cart">
                                '.$cart_block.'
                            </div>
                        </div>
                    </div>
                ';
            }
            if(isset($bottom_right['cart']) && strlen($bottom_right['cart']) > 1) {
                $vars['bottom_right'] .= '

                    <div class="headecart small_widget">
                        <div class="widget_activation">
                            <a href="#" data-box="headcart" class="open-widget nr">'.t('Shoping Cart').'</a><span class=""></span>
                        </div>
                        <div class="top_nav_sub headcart">
                            <div class="cart">
                                '.$cart_block.'
                            </div>
                        </div>
                    </div>
                ';
            }
        }
        if(module_exists('locale')) {
            if(isset($bottom_left['language']) && strlen($bottom_left['language']) > 1) {
                $vars['bottom_left'] .= '
                <div id="lang_sel">
                    '.$lang_block.'
                </div> ';
            }
            if(isset($bottom_right['language']) && strlen($bottom_right['language']) > 1) {
                $vars['bottom_right'] .= '
                    <div id="lang_sel">
                        '.$lang_block.'
                    </div> ';
            }
        }

        if(strlen($bottom_left['social']) > 1) {
            $vars['bottom_left'] .= '<h3 style="float:left;">'.t('Follow Us On').':</h3>'.'<div class="pull-right">'.$social_block.'</div>';
        }
        if(strlen($bottom_right['social']) > 1) {
            $vars['bottom_right'] .= '<h3 style="float:left;">'.t('Follow Us On').':</h3>'.'<div class="pull-right">'.$social_block.'</div>';
        }
        if(strlen($bottom_left['address']) > 1) {
            $vars['bottom_left'] .= '<h3>'.$address_block_1.$address_block_2.'</h3>';
        }
        if(strlen($bottom_right['address']) > 1) {
            $vars['bottom_right'] .= '<h3>'.$address_block_1.$address_block_2.'</h3>';
        }

        drupal_add_css(BRT_BLOCK_PATH.'/assets/css/header-front.css');
    }
}
function brt_block_preprocess_solveto_twitter(&$vars) {
    $brt_data = isset($vars['brt_block']) ? $vars['brt_block'] : null;
    if($brt_data != null) {
        $theme_path = drupal_get_path('theme','solveto');
        $twitter_account = $brt_data['twitter_username'];
        $twitter_max = $brt_data['twitter_num'];
        $twitter_tck = $brt_data['twitter_consumer_key'];
        $twitter_tcs = $brt_data['twitter_consumer_secret'];
        $twitter_tat = $brt_data['twitter_access_token'];
        $twitter_tats = $brt_data['twitter_access_token_secret'];
        if(isset($twitter_account) && isset($twitter_max)){
            drupal_add_js(array('twitter_account'=>$twitter_account),'setting');
            drupal_add_js(array('twitter_num'=>$twitter_max),'setting');
            drupal_add_js(array('tck'=>$twitter_tck),'setting');
            drupal_add_js(array('tcs'=>$twitter_tcs),'setting');
            drupal_add_js(array('tat'=>$twitter_tat),'setting');
            drupal_add_js(array('tats'=>$twitter_tats),'setting');
            drupal_add_js(drupal_get_path('module', 'brt_block') . '/types/twitter/js/jquery.tweet.js');
            drupal_add_js(drupal_get_path('module', 'brt_block') . '/types/twitter/js/script.js');
        }
    }
}
function brt_block_preprocess_solveto_flickr(&$vars) {
    $brt_data = isset($vars['brt_block']) ? $vars['brt_block'] : null;
    if($brt_data != null) {
        drupal_add_js(array('flickr' => array('flickrID' => $brt_data['flickr_id'],'flickrNum' => $brt_data['flickr_num'])),'setting');
        drupal_add_js(drupal_get_path('module', 'brt_block') . '/types/flickr/js/jquery.flickrfeed.min.js');
        drupal_add_js(drupal_get_path('module', 'brt_block') . '/types/flickr/js/flickr.custom.js');
    }
}
function brt_block_preprocess_solveto_video(&$vars) {
    $brt_data = isset($vars['brt_block']) ? $vars['brt_block'] : null;
    $render = '';
    if($brt_data != null) {
        if($brt_data['video_media']) {
            if($brt_data['video_media']['fid'] != 0) {
                $file = file_load($brt_data['video_media']['fid']);
                if (strpos($file->filemime, 'video/') !== 0) {
                    return;
                }
                $key = 'media_' . substr($file->filemime, 6) . '_video';
                $formatter_info = file_info_formatter_types($key);
                $content = array();
                $content['#theme'] = $key;
                $content['#uri'] = $file->uri;
                if (isset($formatter_info['default settings'])) {
                    $content['#options'] = $formatter_info['default settings'];
                }
                $render = drupal_render($content);
            }
        }
    }
    $vars['content'] = $render;
}
function brt_block_preprocess_solveto_instagram(&$vars) {
    $brt_data = isset($vars['brt_block']) ? $vars['brt_block'] : null;
    $render = '';
    if($brt_data != null) {
        // Get configuration values.
        $values = variable_get('instagram_block_data', array());
        $img_resolution = isset($values['img_resolution']) ? $values['img_resolution'] : 'thumbnail';
        $content = array();
        // Check that block configuration is available.
        if (!isset($values['user_id']) || !isset($values['access_token'])) {
            // Remind user to fill in configuration.
            $content = brt_block_instagram_get_configuration_reminder();
        }
        else {
            $url = "https://api.instagram.com/v1/users/" . $values['user_id'] . "/media/recent/?access_token=" . $values['access_token'] . "&count=" . $values['count'];
            $response = drupal_http_request($url, array());
            $result = json_decode($response->data);
            $data = $result->data;
            foreach ($data as $key => $post) {
                $content['children'][$post->id] = array(
                    '#markup' => '',
                    '#theme' => 'solveto_instagram_image',
                    '#post' => $post,
                    '#href' => $post->link,
                    '#src' => $post->images->{$img_resolution}->url,
                    '#width' => $values['width'],
                    '#height' => $values['height'],
                );
            }
        }
    }
    $render = render($content);
    $vars['content'] = $render;
}

/**
 * Builds render array to remind user to configure block settings.
 */
function brt_block_instagram_get_configuration_reminder() {
    return array(
        '#markup' => t('Please don\'t forget to configure this block.')
    );
}
// Function render css options for overlay
function _brt_block_overlay_settings_to_css($brt_data,$name) {
    $output = '';
    if(!empty($name)) {
        $output .= 'background: rgba(';
        $color = hex2RGB($brt_data[$name.'_overlay_colorpicker']);
        if(!empty($color)) {
            $output .= $color['red'].','.$color['green'].','.$color['blue'].',';
        }
        $overlay = !empty($brt_data[$name.'_overlay_opacity']) ? ($brt_data[$name.'_overlay_opacity']/100) : 0.8;
        $output .= $overlay;
        $output .= ')';
    }
    return $output;
}
function _is_youtube($url)
{
    return (preg_match('/youtu\.be/i', $url) || preg_match('/youtube\.com\/watch/i', $url));
}

function _is_vimeo($url)
{
    return (preg_match('/vimeo\.com/i', $url));
}

function _brt_get_youtube_video_id($url)
{
  if(_is_youtube($url))
  {
      $pattern = '/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/';
      preg_match($pattern, $url, $matches);
      if (count($matches) && strlen($matches[7]) == 11)
      {
          return $matches[7];
      }
  }

  return '';
}

function _brt_get_vimeo_video_id($url)
{
    if(_is_vimeo($url))
    {
        $pattern = '/\/\/(www\.)?vimeo.com\/(\d+)($|\/)/';
        preg_match($pattern, $url, $matches);
        if (count($matches))
        {
            return $matches[2];
        }
    }

    return '';
}